package org.monster.context.reader;

import org.dom4j.Element;
import org.monster.context.reader.tag.FilterTag;
import org.monster.context.reader.tag.ServletTag;
import org.monster.context.reader.tag.XmlTag;

import java.util.List;

/**
 * 网xml阅读器
 *
 * @param <E>
 * @author Monster
 */
public class WebXmlReader<E> extends XmlReader {

    /**
     * 服务者映射
     */
    private static final String SERVLET_MAPPING = "servlet-mapping";
    /**
     * 过滤器映射
     */
    private static final String FILTER_MAPPING = "filter-mapping";
    /**
     * url模式
     */
    private static final String URL_PATTERN = "url-pattern";
    /**
     * 过滤器类
     */
    private static final String FILTER_CLASS = "filter-class";
    /**
     * 过滤器名
     */
    private static final String FILTER_NAME = "filter-name";
    /**
     * 过滤器
     */
    private static final String FILTER = "filter";
    /**
     * 服务者
     */
    private static final String SERVLET = "servlet";
    /**
     * 服务者类
     */
    private static final String SERVLET_CLASS = "servlet-class";
    /**
     * 服务者名
     */
    private static final String SERVLET_NAME = "servlet-name";
    /**
     * 服务者标签
     */
    private ServletTag servletTag = new ServletTag();
    /**
     * 过滤器标签
     */
    private FilterTag filterTag = new FilterTag();

    /**
     * @param origin        原元素
     * @param originTagName 原标标签
     * @param target        目标元素
     * @param targetTagName 目标元素标签
     * @return
     */
    public boolean nameEquals(Element origin, String originTagName,
                              Element target, String targetTagName) {
        return origin.getName().equals(originTagName)
                && target.getName().equals(targetTagName);
    }

    /**
     * 是否 空
     *
     * @param servletTag 服务者标签
     * @return 布尔
     */
    public boolean isNull(ServletTag servletTag) {
        return servletTag.getMappingServletName() == null
                || servletTag.getMappingUrlPattern() == null
                || servletTag.getServletName() == null
                || servletTag.getServletClass() == null;
    }

    /**
     * 是否 过滤器标签
     *
     * @param filterTag 过滤器标签
     * @return 过滤器标签
     */
    public boolean isNull(FilterTag filterTag) {
        return filterTag.getFilterName() == null
                || filterTag.getFilterClass() == null
                || filterTag.getMappingFilterName() == null
                || filterTag.getMappingFilterUrlPattern() == null;
    }

    /**
     * 解析元素
     * 解析webxml的Element装到父类xmltags容器
     *
     * @param element 元素
     */
    @Override
    public void resolveElement(Element element) {
        List<Element> eleOnes = element.elements();
        for (Element eleTwo : eleOnes) {
            List<Element> eleThirds = eleTwo.elements();
            for (Element eleThird : eleThirds) {
                if (nameEquals(eleThird, SERVLET_NAME, eleTwo, SERVLET)) {
                    servletTag.setServletName(eleThird.getText());
                }
                if (nameEquals(eleThird, SERVLET_CLASS, eleTwo, SERVLET)) {
                    servletTag.setServletClass(eleThird.getText());
                }
                if (nameEquals(eleThird, SERVLET_NAME, eleTwo, SERVLET_MAPPING)) {
                    servletTag.setMappingServletName(eleThird.getText());
                }
                if (nameEquals(eleThird, URL_PATTERN, eleTwo, SERVLET_MAPPING)) {
                    servletTag.setMappingUrlPattern(eleThird.getText());
                }
                if (nameEquals(eleThird, FILTER_NAME, eleTwo, FILTER)) {
                    filterTag.setFilterName(eleThird.getText());
                }
                if (nameEquals(eleThird, FILTER_CLASS, eleTwo, FILTER)) {
                    filterTag.setFilterClass(eleThird.getText());
                }
                if (nameEquals(eleThird, FILTER_NAME, eleTwo, FILTER_MAPPING)) {
                    filterTag.setMappingFilterName(eleThird.getText());
                }
                if (nameEquals(eleThird, URL_PATTERN, eleTwo, FILTER_MAPPING)) {
                    filterTag.setMappingFilterUrlPattern(eleThird.getText());
                }
            }
            if (!isNull(servletTag)) {
                getXmlTags().add(servletTag);
                servletTag = new ServletTag();
            }
            if (!isNull(filterTag)) {
                getXmlTags().add(filterTag);
                filterTag = new FilterTag();
            }
        }
    }

    /**
     * 获取 标签
     * 返回匹配URI的ServletTag
     *
     * @param name 名
     * @return 标签
     */
    @Override
    public XmlTag getTag(String name) {
        for (Object object : getXmlTags()) {
            if (object instanceof ServletTag) {
                servletTag = (ServletTag) object;
                if (servletTag.getMappingUrlPattern().equals(name)) {
                    return servletTag;
                }
            }
        }
        return null;
    }

    /**
     * 获取 标签
     *
     * @return 标签 集合
     */
    @Override
    public List<E> getTags() {
        return getXmlTags();
    }

}
